//  Inside Microsoft Exchange 2007 Web Services Chapter 4
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.IO;
using ProxyHelpers.EWS;
using System.Diagnostics;

namespace GetFolderViaProxy
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            BaseFolderType[] folders = GetInboxAndDeletedItemsFolders(binding);
            foreach (BaseFolderType folder in folders)
            {
                Console.WriteLine(folder.DisplayName);
            }
        }

        /// <summary>
        /// Return the inbox and deleted items folders (Listing 4-2)
        /// </summary>
        /// <param name="binding">Binding which is used to call the EWS web service</param>
        /// <returns>Array containing the inbox and deleted items folders</returns>
        /// 
        public static BaseFolderType[] GetInboxAndDeletedItemsFolders(ExchangeServiceBinding binding)
        {
            // Create our request object and set the response shape
            //
            GetFolderType request = new GetFolderType();
            request.FolderShape = new FolderResponseShapeType();
            request.FolderShape.BaseShape = DefaultShapeNamesType.Default;

            // Set the folders that we wish to request
            //
            DistinguishedFolderIdType inboxId = new DistinguishedFolderIdType();
            inboxId.Id = DistinguishedFolderIdNameType.inbox;
            DistinguishedFolderIdType deletedItemsId = new DistinguishedFolderIdType();
            deletedItemsId.Id = DistinguishedFolderIdNameType.deleteditems;
            request.FolderIds = new BaseFolderIdType[] { inboxId, deletedItemsId };

            // Now make the actual request
            //
            GetFolderResponseType response = binding.GetFolder(request);

            // There should be two response messages - one for each folder we requested
            //
            Debug.Assert(response.ResponseMessages.Items.Length == 2);
            BaseFolderType[] result = new BaseFolderType[2];
            FolderInfoResponseMessageType firstFolderResponse =
               (FolderInfoResponseMessageType)response.ResponseMessages.Items[0];
            result[0] = firstFolderResponse.Folders[0];

            FolderInfoResponseMessageType secondFolderResponse =
               (FolderInfoResponseMessageType)response.ResponseMessages.Items[1];
            result[1] = secondFolderResponse.Folders[0];
            return result;
        }
    }
}
